package fr.emse.tatiana.replayable;

import java.util.List;

import org.w3c.dom.Node;

import fr.emse.tatiana.display.Ut;

public class Anchor {
	
	private Node node;
	private String str;
	
	public Anchor(Node node) {
		this.node=node;
		this.str=Ut.formatXMLNode(node);
	}
	
	public Anchor(String path, String s) {
		this(
		        "<doc>"+path+"</doc>" +
		        "<path>id["+s+"]</path>");
	}
	
	public Anchor(String path, int i) {
		this(
        "<doc>"+path+"</doc>" +
        "<path>/display/item["+i+"]</path>");
	}
	
	public Anchor(String contents) {
		this.str="<anchor>" + contents + "</anchor>";
	}

	public Anchor(List<Anchor> anchors) {
		if (anchors.size() == 1) {
			this.str=anchors.get(0).toString();
		} else {
			this.str="<anchor><set>";
			for (Anchor anchor : anchors) {
				this.str+=anchor.toString();
			}
			this.str+="</set></anchor>";
		}
	}
	
	@Override
	public String toString() {
		return str;
	}

	@Override
	public int hashCode() {
		return toString().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Anchor other = (Anchor) obj;
		return this.toString().equals(other.toString());
	}
	
	public boolean subOrSuperSet(Anchor a) {
		return this.toString().contains(a.toString()) || a.toString().contains(this.toString());
	}
	
}
